package player.ui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;



public class VideoPanel extends JLayeredPane{
	private static final long serialVersionUID = 2045997518116043384L;
	PlayerFrame frame;
	Insets gap=new Insets(0,0,0,0);
	Point p   =  new Point(); 
	VideoLayout videoLayout;
	VideoCanvas videoCanvas;
	ThumbPanel thumbPanel;
	VideoPanel(final PlayerFrame frame){
		super();
		this.frame=frame;
		setBackground(new Color(150,150,150));
		addListener();


		thumbPanel=new ThumbPanel();
		videoCanvas=new VideoCanvas();
		videoLayout=new VideoLayout(this,videoCanvas,thumbPanel);
		setLayout(videoLayout);

		
		
		//setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		Point origin = new Point(10, 20);
		 
		// This is the offset for computing the origin for the next label.
		int offset = 35;
		 int NUM_LAYERS = 5;
		for (int i = 0; i < NUM_LAYERS; i++) {
			JComponent component = createColoredBox2(i,
					origin);
			add(component, new Integer(i));
			origin.x += offset;
			origin.y += offset;
		}

		add(thumbPanel,10);
//
		add(videoCanvas,JLayeredPane.DEFAULT_LAYER);
		
		setLayer(videoCanvas,-1);
		//this.moveToBack(videoCanvas);
	}
	
	private JComponent createColoredBox2(int layer, Point origin) {
		
		JPanel panel = new VideoCanvas();
		panel.setLayout(new GridLayout());
		//panel.setAlpha(.5f);
		//panel.add(new JButton("Button on Layer: " + layer));
		panel.setBounds(origin.x, origin.y, 140, 140);
		return panel;
	}
	
	private JComponent createColoredBox(int layer, Point origin) {
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout());
		//panel.setAlpha(.5f);
		panel.add(new JButton("Button on Layer: " + layer));
		panel.setBounds(origin.x, origin.y, 140, 140);
		return panel;
	}
	
	public VideoPanel(){
		this(null);
	}
	
	void addListener(){
		
		addMouseWheelListener(new MouseWheelListener(){
			
			public void mouseWheelMoved(MouseWheelEvent e) {
				System.out.println(e);
				frame.changeSize(e,4/3f);
			}
			
		});
		addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent e) {
				
			}

			public void mouseEntered(MouseEvent e) {
				
			}

			public void mouseExited(MouseEvent e) {
				
			}

			public void mousePressed(MouseEvent e) {
				p=e.getPoint(); 
				frame.requestFocus();
				frame.toFront();
			}

			public void mouseReleased(MouseEvent e) {
				
			}
			
		});
		addMouseMotionListener(new MouseMotionListener(){

			public void mouseDragged(MouseEvent e) {
				Point   p1   =   e.getPoint();
				Point   p2   =   frame.getLocation(null);
				p2.x   +=   p1.x   -   p.x;
				p2.y   +=   p1.y   -   p.y;
				frame. setLocation(p2); 
			}

			public void mouseMoved(MouseEvent e) {
				
			}
			
		});
	}
	
	
}
